package com.jic.member.impl;
import	java.util.Date;

import java.lang.Long;
import java.util.List;
import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.member.entity.Meinformation;
import com.jic.member.mapper.MeinformationMapper;
import com.jic.member.request.MeinformationRequest;
import com.jic.member.response.MeinformationResponse;
import com.jic.member.service.MeinformationService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 个人消息表
 * @author : zxl
 * @email : 
 * @since : 2020-08-10 03:47:46
 * @version : v1.0.0
 */
@Service
public class MeinformationServiceImpl implements MeinformationService{

    @Resource
    private MeinformationMapper meinformationMapper;

    @Override
    public long insert(Meinformation meinformation) {
        long flag = meinformationMapper.insert(meinformation);
        return flag;
    }

    @Override
    public long delete(Long id) {
        return meinformationMapper.delete(id);
    }

    @Override
    public long update(Meinformation meinformation) {
        return meinformationMapper.update(meinformation);
    }

    @Override
    public Meinformation queryByPrimaryKey(Long id){
        return meinformationMapper.queryByPrimaryKey(id);
    }

    @Override
    public List<Meinformation> query(Meinformation meinformation){
        return meinformationMapper.query(meinformation);
    }

    @Override
    public PageResult<Meinformation> queryPage(Meinformation meinformation, Page pageQuery) {
        //计算下标
        int startIndex = (pageQuery.getStart() - 1) * pageQuery.getLimit();
        List<Meinformation> list = meinformationMapper.queryPage(startIndex, pageQuery.getLimit(), meinformation);
        long count = meinformationMapper.queryPageCount(meinformation);
        PageResult pageResult = new PageResult();
        pageResult.setRows(list);
        pageResult.setTotal(count);
        return pageResult;
    }

    @Override
    public RestResult insertInformation(Meinformation memberInformation) {
        RestResult result = new RestResult();
        if (null != memberInformation.getId() && memberInformation.getId() !=0){
            memberInformation.setUpdateTime(new Date());
            meinformationMapper.update(memberInformation);
        }else {
            meinformationMapper.insert(memberInformation);
        }
        result.setCode("0000");
        return result;
    }

    @Override
    public RestResult queryMemberInformationInfo(MeinformationRequest form) {
        Meinformation meinformation =new Meinformation();
        BeanUtils.copyProperties(form,meinformation);
        List<Meinformation> result = meinformationMapper.query(meinformation);
        return RestResult.success(result);
    }

    @Override
    public RestResult<PageResult<MeinformationResponse>> getMemberInfoList(MeinformationRequest meinformationRequest) {
        Meinformation meinformation =new Meinformation();
        BeanUtils.copyProperties(meinformationRequest,meinformation);
        meinformation.setDeleteFlag(0);
        //计算下标
        int startIndex = (meinformationRequest.getStart() - 1) * meinformationRequest.getLimit();
        List<Meinformation> list = meinformationMapper.queryPage(startIndex, meinformationRequest.getLimit(), meinformation);
        long count = meinformationMapper.queryPageCount(meinformation);
        PageResult pageResult = new PageResult();
        pageResult.setRows(list);
        pageResult.setTotal(count);
        return RestResult.success(pageResult);
    }

    @Override
    public RestResult<String> deleteMemberInfo(MeinformationRequest meinformationRequest) {
        RestResult result = new RestResult();
        Long[] ids = meinformationRequest.getIds();
        for (int i = 0; i < ids.length; i++){
            long delete = meinformationMapper.delete(ids[i]);
        }
        result.setCode("0000");
        return result;
    }

    @Override
    public RestResult<String> insertMemberInformation(MeinformationRequest meinformationRequest) {
        Meinformation meinformation =new Meinformation();
        BeanUtils.copyProperties(meinformationRequest,meinformation);
        meinformation.setIsRead(2);
        long insert = meinformationMapper.insert(meinformation);
        return RestResult.success("1");
    }

    @Override
    public RestResult<String> updateMemberInformation(MeinformationRequest meinformationRequest) {
        Meinformation meinformation =new Meinformation();
        BeanUtils.copyProperties(meinformationRequest,meinformation);
        Long[] ids = meinformationRequest.getIds();
        for (int i = 0; i < ids.length; i++){
            meinformation.setId(ids[i]);
            long update = meinformationMapper.update(meinformation);
        }
        return RestResult.success("1");
    }
}